/* 
 * Copyright [2017] [Alex/LiBo(libo2dev.aliyun.com/alex.link@foxmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.bmsp.record;

import java.util.Arrays;
import java.util.Objects;

import org.apache.commons.lang.StringUtils;

/**  
 * @Title PageQuery.java  
 * @Package com.hyts.bmsp.record  
 * @Description 分页查询操作服务类  
 * @author LiBo/Alex  
 * @date 2017年12月24日  
 * @version V1.0  
 */
public class PageQuery extends BaseQuery{
	
	/**  
	 * @Fields field:field:sql语句前缀
	 */ 
	private String sqlPrefix;
	
	/**  
	 * @Fields field:field:sql查询条件 
	 */ 
	private String sqlSubfix;
	
	/**  
	 * @Fields field:field:返回字段
	 */ 
	private String[] returnFields;
	
	/**  
	 * @Fields field:field:表名  
	 */ 
	private String tableName;
	
	/**  
	 * @Fields field:field 条件
	 */ 
	private String[] condition;
	
	/**  
	 * @Fields field:field:是否进行分组操作  
	 */ 
	private boolean isGroup = false;
	
	private int pageSize;
	
	private int pageNo;
	
	/**  
	 * @return sqlPrefix  
	 */
	protected String getSqlPrefix() {
		StringBuilder sql = new StringBuilder("SELECT ");
		if(Objects.nonNull(this.returnFields) 
				&& (this.returnFields.length != 0)){
			Arrays.stream(this.returnFields).forEach(param->{
				sql.append(param).append(",");
			});
			return sql.substring(0,sql.length()-1);
		}
		return sql.append(" *").toString();
	}

	/**  
	 * @return sqlSubfix  
	 */
	protected String getSqlSubfix() {
		StringBuilder sql = new StringBuilder(" FROM ").append(tableName).append(" WHERE 1 = 1 ");
		if(Objects.isNull(this.condition)
				|| (this.condition.length == 0)){
			Arrays.stream(this.condition).forEach(param->{
				sql.append(param);
			});
		}
		return sql.toString();	
	}

	/**  
	 * @return pageSize  
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**  
	 * @param paramtheparamthe{bare_field_name} to set  
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**  
	 * @return pageNo  
	 */
	public int getPageNo() {
		return pageNo;
	}

	/**  
	 * @param paramtheparamthe{bare_field_name} to set  
	 */
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	/**  
	 * 创建一个新的实例-构造器 PageQuery.  
	 * @param returnFields
	 * @param tableName
	 * @param condition
	 * @param isGroup
	 * @param pageSize
	 * @param pageNo  
	 */ 
	protected PageQuery(String[] returnFields, String tableName, String[] condition, boolean isGroup, int pageSize,
			int pageNo) {
		super();
		this.returnFields = returnFields;
		this.tableName = tableName;
		this.condition = condition;
		this.isGroup = isGroup;
		this.pageSize = pageSize;
		this.pageNo = pageNo;
	}

	/**  
	 * 创建一个新的实例-构造器 PageQuery.  
	 * @param returnFields
	 * @param tableName
	 * @param condition
	 * @param isGroup  
	 */ 
	protected PageQuery(String[] returnFields, String tableName, String[] condition, boolean isGroup) {
		super();
		this.returnFields = returnFields;
		this.tableName = tableName;
		this.condition = condition;
		this.isGroup = isGroup;
	}

	/**  
	 * 创建一个新的实例-构造器 PageQuery.  
	 * @param returnFields
	 * @param tableName
	 * @param condition  
	 */ 
	protected PageQuery(String[] returnFields, String tableName, String[] condition) {
		super();
		this.returnFields = returnFields;
		this.tableName = tableName;
		this.condition = condition;
	}

	/**  
	 * @return isGroup  
	 */
	public boolean isGroup() {
		return isGroup;
	}

	/**  
	 * @param paramtheparamthe{bare_field_name} to set  
	 */
	public void setGroup(boolean isGroup) {
		this.isGroup = isGroup;
	}
	
}
